<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="userInfo.userName"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="userInfo.pwd"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      userInfo: {
        userName: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit (data) {
      // console.log(data)
      // console.log(this.userInfo)
      setTimeout(() => {
        // 请求成功了
        const userInfo = {
          name: '小化',
          avatar: 'http://himg.bdimg.com/sys/portrait/item/d1846c697869616e676d616769638c27.jpg',
          gender: 0
        }
        const token = 'dhwidjwi493dr'
        this.set_userInfo(userInfo)
        this.set_token(token)
        localStorage.setItem('token', token)
        if (this.$route.params.from) {
          // 路由拦截重定向
          this.$router.push(this.$route.params.from)
        } else {
          this.$router.push('/home')
        }
        /*
        别忘了退出登录时，除了 清除vuex中的用户信息的数据，还要手动清除缓存中的数据
        */
      }, 2000)
    },
    ...mapMutations('user', ['set_userInfo', 'set_token'])
  }
}
</script>

<style lang="scss" scoped>

</style>
